import useDictStore from '@/store/modules/dict'
import { getDicts } from '@/api/system/dict/data'

export function useDict(...args) {
  const dicts = {}

  args.forEach(dictType => {
    // 初始化字典数据
    dicts[dictType] = []

    // 检查 store 中是否有缓存
    const cachedDict = useDictStore().getDict(dictType)
    if (cachedDict) {
      dicts[dictType] = cachedDict
    } else {
      // 没有缓存则发起请求
      getDicts(dictType).then(resp => {
        const dictData = resp.data.map(p => ({
          label: p.dictLabel,
          value: p.dictValue,
          elTagType: p.listClass,
          elTagClass: p.cssClass
        }))

        // 更新字典数据
        dicts[dictType] = dictData
        // 将字典存入 store
        useDictStore().setDict(dictType, dictData);
      })
    }
  })

  return dicts
}
